<template>
  <main-box>
    <PlusSearch
      v-model="queryform"
      :columns="formConfig"
      :show-number="2"
      label-width="80"
      label-position="right"
      class="mb-xs"
      @search="initData()"
      @reset="handleRest()"
    >
      <template #plus-field-result>
        <el-radio-group v-model="queryform.result">
          <el-radio-button label="全部" :value="-1" />
          <el-radio-button label="正常" :value="1" />
          <el-radio-button label="异常" :value="2" />
        </el-radio-group>
      </template>
    </PlusSearch>
    <div class="mb-2">
      <btnExport :exportApi="exportApi" :queryform="getQueryParam" />
    </div>
    <PureTable
      ref="tableRef"
      border
      adaptive
      :loading="loading"
      :data="tableData"
      :columns="columns"
      :pagination="pagination"
      @page-size-change="initData()"
      @page-current-change="initData(false)"
    />
  </main-box>
</template>

<script setup lang="tsx">
import { ref } from "vue";
import { useTableConfig } from "@/utils/tableConfig";
import { type PlusColumn, PlusSearch } from "plus-pro-components";

import { listApi, exportApi } from "@/api/depot/stockTake/tools";
import btnExport from "@/components/btnExport/index.vue";
import { useRoute } from "vue-router";
import { stockTakeResult } from "@/utils/dict";
/* 查询表单 */
const formConfig: PlusColumn[] = [
  {
    label: "盘点结果",
    prop: "result"
  },
  {
    label: "查询",
    prop: "searchValue",
    fieldProps: { placeholder: "物品名称/编号/分类/编码" }
  }
];
const queryform = ref({
  result: -1,
  searchValue: ""
});

const handleRest = () => {
  initData(true);
  queryform.value.result = -1;
};
const route = useRoute();
const getQueryParam = () => {
  const params = {
    ...queryform.value,
    facilityRecordId: route.query.facilityRecordId
  } as any;
  params.result = params.result < 0 ? "" : params.result;
  return params;
};

/* 表格数据 */
const { pagination } = useTableConfig();
const tableData = ref([]);
const loading = ref(false);
const columns = [
  {
    label: "物件图片",
    prop: "picUrl",
    cellRenderer: ({ row }) => {
      const { picUrl } = row;
      let list = [];
      if (picUrl) {
        list = picUrl.split(",");
      }
      return list.map((item, index) => {
        return (
          <el-image
            src={item}
            width={50}
            initial-index={index}
            preview-src-list={list}
            preview-teleported={true}
          />
        );
      });
    }
  },
  { label: "物件编号", prop: "code" },
  { label: "物件名称", prop: "name" },
  { label: "分类", prop: "toolCategoryName" },
  { label: "品牌", prop: "brandName" },
  { label: "标签编号", prop: "labelCode" },
  { label: "工具室", prop: "warehouseName" },
  { label: "存放位置", prop: "storageLocation" },
  {
    label: "盘点结果",
    prop: "result",
    cellRenderer: ({ row }) => {
      const target = stockTakeResult(row.result);
      return <el-tag type={target.type}>{target.name}</el-tag>;
    }
  },
  { label: "盘点时间", prop: "stockTakeTime" }
];

const initData = async (needInit = true) => {
  loading.value = true;
  if (needInit) {
    pagination.currentPage = 1;
  }
  const params = {
    pageNum: pagination.currentPage,
    pageSize: pagination.pageSize,
    ...getQueryParam()
  } as any;
  await listApi(params).then(res => {
    tableData.value = res.rows;
    pagination.total = Number(res.total);
  });
  loading.value = false;
};
initData();
</script>
